<?
require_once("class_GenericMultiComposit.php");
require_once("class_SchemaName.php");

/********************************************************************************************************/
/* This class represents one row from any table which hase non composit not autoincremented PRIMARY KEY */
/********************************************************************************************************/

class DatabaseName extends GenericMultiComposit {
    protected $prod_sql;

    public function DatabaseName($dn_id=false) {
	parent::__construct('database_name','id',$dn_id);
	$this->add_composit('schema_name','SchemaName','dn_id','id');
	$this->prod_sql=false;
    }

    public function get_conn_string() {
	$this->sql->select_c("SELECT get_conn_string(".$this->get_field('hc_id').",".$this->id.") AS conn_string");
	$string=$this->sql->get_row_hash();
	return $string['conn_string'];
    }


    public function populate_db_stat($time_id) {
	if ($this->get_field('observable') == 't') {
	    if(!$this->prod_sql) {
		$this->prod_sql=new SQL($this->get_conn_string());
	    }

	    $this->prod_sql->select_c("
SELECT
pg_database_size(oid) AS db_size,
pg_stat_get_db_xact_commit(oid) AS xact_commit,
pg_stat_get_db_xact_rollback(oid) AS xact_rollback,
pg_stat_get_db_blocks_fetched(oid) AS blks_fetch,
pg_stat_get_db_blocks_hit(oid) AS blks_hit,
pg_stat_get_db_tuples_returned(oid) AS tup_returned,
pg_stat_get_db_tuples_fetched(oid) AS tup_fetched,
pg_stat_get_db_tuples_inserted(oid) AS tup_inserted,
pg_stat_get_db_tuples_updated(oid) AS tup_updated,
pg_stat_get_db_tuples_deleted(oid) AS tup_deleted
FROM pg_database
WHERE oid =".$this->get_field('obj_oid'));
	    $db_stat=$this->prod_sql->get_row_hash();
	    $this->sql->non_select_c("INSERT INTO database_stat VALUES (".$this->id.",".$time_id.",".
	$db_stat['db_size'].",".
	$db_stat['xact_commit'].",".
	$db_stat['xact_rollback'].",".
	$db_stat['blks_fetch'].",".
	$db_stat['blks_hit'].",".
	$db_stat['tup_returned'].",".
	$db_stat['tup_fetched'].",".
	$db_stat['tup_inserted'].",".
	$db_stat['tup_updated'].",".
	$db_stat['tup_deleted'].")");

	}
    }

    public function get_prod_sql() {
	if (!$this->prod_sql) {
	    $this->prod_sql=new SQL($this->get_conn_string());
	}
	return $this->prod_sql;
    }

/*
    public function discover() {
	if(!$this->prod_sql) {
	    $this->prod_sql=new SQL($this->get_conn_string());
	}

	$this->prod_sql->select_c("SELECT oid,nspname
	    FROM pg_namespace
	    WHERE nspname NOT IN ('pg_catalog', 'information_schema')
	    AND nspname !~ '^pg_toast'
	    AND nspname !~ '^pg_temp'");

	$web_ui_discover_condition=array('alive'=>'t');
	$this->initiate_composit('schema_name',$web_ui_discover_condition);

	$exist_ids=$this->get_composit('schema_name',COLLECTION_ID_ARRAY);
	$ret=array();

	if (!$this->prod_sql) {
	    $this->prod_sql=new SQL($this->get_conn_string);
	}

	foreach ($this->prod_sql->get_result() as $prod_sch) {
	    $exist_id=$this->composit_obj_exists('schema_name','obj_oid',$prod_sch['oid']);
	    if (!$exist_id) {
		$sn=new SchemaName;
		$sn->set_field('sch_name',$prod_sch['nspname']);
		$sn->set_field($this->composit_array['schema_name']['link_fk'],$this->id);
		$sn->set_field('obj_oid',$prod_sch['oid']);
		$sn->set_field('observable','t'); //todo: Use defined constant for config purposes 
		$sn->save();
		$this->add_composit_object('schema_name',$sn->get_id());
		$ret['added'][]=$prod_sch['nspname'];
	    } else {
		foreach ($exist_ids as $id=>$val) {
		    if ($val == $exist_id) {
			unset($exist_ids[$id]);
		    }
		}
	    }
	}
	foreach ($exist_ids as $old_id) {
	    $old_sn=new SchemaName($old_id);
	    $old_sn->set_field('alive','f');
	    $old_sn->save();
	    $this->remove_composit_object('schema_name',$old_id);

	    $ret['removed'][]=$old_sn->get_field('sch_name');
	}
	return $ret;
    }



    public function update_database($changes) {
	$old=$this->get_all_fields();
	if ($old['description'] != $changes['edit_dn__description']) {
	    $this->set_field('description',$changes['edit_dn__description']);
	}
	if ($old['observable'] != $changes['edit_dn__observable']) {
	    $this->set_field('observable',$changes['edit_dn__observable']);
	}
	$this->save();
    }




    public function print_config_table_row($added) {
	if (!$this->loaded) {
	    $this->load();
	}
	if (in_array($this->database_fields['db_name'],$added)) {
	    $string="<tr bgcolor=#DDD>";
	} else {
	    $string="<tr>";
	}
	if ($this->database_fields['observable']=='t') {
	    $string.="<td><a href=".$_SERVER['PHP_SELF']."?level=2&content=schema_name&dn_id=".$this->id.">".$this->database_fields['db_name']."</a></td>";
	} else {
	    $string.="<td>".$this->database_fields['db_name']."</td>";
	}
	$string.="<td>".$this->database_fields['description']."</td>
	    <td align=center>".($this->database_fields['observable']=='t' ? 'YES' : '<i>No</i>')."</td>
	    <td align=right><a href=".$_SERVER['PHP_SELF']."?action=edit&content=database_name&dn_id=".$this->id."><i>Edit</i></a></td>";
	$string.="</tr>";
	return $string;
    }

    public function __toString() {
	return $this->get_field('db_name');
    }
*/
}

?>
